package com.xxmw.transaction.controller;


import com.xxmw.transaction.common.entity.P;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.model.ScheduleJob;
import com.xxmw.transaction.service.job.ScheduleJobService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 定时任务列表
 * @since  2020/2/18 18:29
 * @author luowenliang
 */
@RestController
@RequestMapping("/admin/schedule")
public class ScheduleJobController extends BaseController{

	@Autowired
	private ScheduleJobService scheduleJobService;

	/**
	 * 定时任务列表
	 * @param pageNum 页码
	 * @param pageSize 页量
	 * @param beanName 类名称
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("system:schedule:list")
	public R list(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
				  @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
				  @RequestParam(required = false) String beanName) {
		P<ScheduleJob> p = scheduleJobService.getJobList(pageNum, pageSize, ScheduleJob.JobType.system.getValue(), null, beanName);
		return R.ok(p);
	}

	/**
	 * 定时任务信息
	 * @param jobId
	 * @return
	 */
	@GetMapping("/info/{jobId}")
	@RequiresPermissions("system:schedule:info")
	public R info(@PathVariable Long jobId) {
		ScheduleJob scheduleJob = scheduleJobService.getScheduleJob(jobId);
		return R.ok().put("schedule", scheduleJob);
	}

	/**
	 * 保存更新
	 * @param scheduleJob
	 * @return
	 */
	@Validated
	@PostMapping(value = "/saveOrUpdate", consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
	@RequiresPermissions(value = {"system:schedule:save","system:schedule:update"}, logical = Logical.OR)
	public R saveOrUpdate(@RequestBody ScheduleJob scheduleJob) {
		if (null == scheduleJob.getJobId() || 0 == scheduleJob.getJobId()) {
			scheduleJob.setCreated(new Date());
			scheduleJob.setType(ScheduleJob.JobType.system.getValue());
		}
		scheduleJobService.addOrUpdate(scheduleJob);
		return R.ok();
	}

	/**
	 * 删除
	 * @param jobIds
	 * @return
	 */
	@PostMapping("/delete")
	@RequiresPermissions("system:schedule:delete")
	public R delete(Long[] jobIds) {
		scheduleJobService.deleteBatch(jobIds);
		return R.ok();
	}

	/**
	 * 立即执行
	 * @param jobIds
	 * @return
	 */
	@PostMapping("/run")
	@RequiresPermissions("system:schedule:run")
	public R run(Long[] jobIds) {
		scheduleJobService.run(jobIds);
		return R.ok();
	}

	/**
	 * 暂停任务
	 * @param jobIds
	 * @return
	 */
	@PostMapping("/pause")
	@RequiresPermissions("system:schedule:pause")
	public R pause(Long[] jobIds) {
		scheduleJobService.pause(jobIds);
		return R.ok();
	}

	/**
	 * 恢复任务
	 * @param jobIds
	 * @return
	 */
	@PostMapping("/resume")
	@RequiresPermissions("system:schedule:resume")
	public R resume(Long[] jobIds) {
		scheduleJobService.resume(jobIds);
		return R.ok();
	}
}
